home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / ds3100.md / gdb / blockframe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-01  |  16.9 KB  |  669 lines

  1. /* Get info from stack frames;
  2.    convert between frames, blocks, functions and pc values.
  3.    Copyright 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "symtab.h"
  23. #include "bfd.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "frame.h"
  27. #include "gdbcore.h"
  28. #include "value.h"        /* for read_register */
  29. #include "target.h"        /* for target_has_stack */
  30. #include "inferior.h"        /* for read_pc */
  31.  
  32. /* Is ADDR inside the startup file?  Note that if your machine
  33.    has a way to detect the bottom of the stack, there is no need
  34.    to call this function from FRAME_CHAIN_VALID; the reason for
  35.    doing so is that some machines have no way of detecting bottom
  36.    of stack.  */
  37.  
  38. int
  39. inside_entry_file (addr)
  40.      CORE_ADDR addr;
  41. {
  42.   if (symfile_objfile) 
  43.   return (addr >= symfile_objfile -> ei.entry_file_lowpc &&
  44.       addr <  symfile_objfile -> ei.entry_file_highpc);
  45.   else return 0;
  46. }
  47.  
  48. /* Test a specified PC value to see if it is in the range of addresses
  49.    that correspond to the main() function.  See comments above for why
  50.    we might want to do this.
  51.  
  52.    Typically called from FRAME_CHAIN_VALID. */
  53.  
  54. int
  55. inside_main_func (pc)
  56. CORE_ADDR pc;
  57. {
  58.   if (symfile_objfile)
  59.   return (symfile_objfile -> ei.main_func_lowpc  <= pc &&
  60.       symfile_objfile -> ei.main_func_highpc > pc);
  61.   else return 0;
  62. }
  63.  
  64. /* Test a specified PC value to see if it is in the range of addresses
  65.    that correspond to the process entry point function.  See comments
  66.    in objfiles.h for why we might want to do this.
  67.  
  68.    Typically called from FRAME_CHAIN_VALID. */
  69.  
  70. int
  71. inside_entry_func (pc)
  72. CORE_ADDR pc;
  73. {
  74.   if (symfile_objfile)
  75.   return (symfile_objfile -> ei.entry_func_lowpc  <= pc &&
  76.       symfile_objfile -> ei.entry_func_highpc > pc);
  77.   else return 0;
  78. }
  79.  
  80. /* Address of innermost stack frame (contents of FP register) */
  81.  
  82. static FRAME current_frame;
  83.  
  84. /*
  85.  * Cache for frame addresses already read by gdb.  Valid only while
  86.  * inferior is stopped.  Control variables for the frame cache should
  87.  * be local to this module.
  88.  */
  89. struct obstack frame_cache_obstack;
  90.  
  91. /* Return the innermost (currently executing) stack frame.  */
  92.  
  93. FRAME
  94. get_current_frame ()
  95. {
  96.   /* We assume its address is kept in a general register;
  97.      param.h says which register.  */
  98.  
  99.   return current_frame;
  100. }
  101.  
  102. void
  103. set_current_frame (frame)
  104.      FRAME frame;
  105. {
  106.   current_frame = frame;
  107. }
  108.  
  109. FRAME
  110. create_new_frame (addr, pc)
  111.      FRAME_ADDR addr;
  112.      CORE_ADDR pc;
  113. {
  114.   struct frame_info *fci;    /* Same type as FRAME */
  115.  
  116.   fci = (struct frame_info *)
  117.     obstack_alloc (&frame_cache_obstack,
  118.            sizeof (struct frame_info));
  119.  
  120.   /* Arbitrary frame */
  121.   fci->next = (struct frame_info *) 0;
  122.   fci->prev = (struct frame_info *) 0;
  123.   fci->frame = addr;
  124.   fci->next_frame = 0;        /* Since arbitrary */
  125.   fci->pc = pc;
  126.  
  127. #ifdef INIT_EXTRA_FRAME_INFO
  128.   INIT_EXTRA_FRAME_INFO (0, fci);
  129. #endif
  130.  
  131.   return fci;
  132. }
  133.  
  134. /* Return the frame that called FRAME.
  135.    If FRAME is the original frame (it has no caller), return 0.  */
  136.  
  137. FRAME
  138. get_prev_frame (frame)
  139.      FRAME frame;
  140. {
  141.   /* We're allowed to know that FRAME and "struct frame_info *" are
  142.      the same */
  143.   return get_prev_frame_info (frame);
  144. }
  145.  
  146. /* Return the frame that FRAME calls (0 if FRAME is the innermost
  147.    frame).  */
  148.  
  149. FRAME
  150. get_next_frame (frame)
  151.      FRAME frame;
  152. {
  153.   /* We're allowed to know that FRAME and "struct frame_info *" are
  154.      the same */
  155.   return frame->next;
  156. }
  157.  
  158. /*
  159.  * Flush the entire frame cache.
  160.  */
  161. void
  162. flush_cached_frames ()
  163. {
  164.   /* Since we can't really be sure what the first object allocated was */
  165.   obstack_free (&frame_cache_obstack, 0);
  166.   obstack_init (&frame_cache_obstack);
  167.  
  168.   current_frame = (struct frame_info *) 0; /* Invalidate cache */
  169. }
  170.  
  171. /* Flush the frame cache, and start a new one if necessary.  */
  172. void
  173. reinit_frame_cache ()
  174. {
  175.   FRAME fr = current_frame;
  176.   flush_cached_frames ();
  177.   if (fr)
  178.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  179.                       read_pc ()));
  180. }
  181.  
  182. /* Return a structure containing various interesting information
  183.    about a specified stack frame.  */
  184. /* How do I justify including this function?  Well, the FRAME
  185.    identifier format has gone through several changes recently, and
  186.    it's not completely inconceivable that it could happen again.  If
  187.    it does, have this routine around will help */
  188.  
  189. struct frame_info *
  190. get_frame_info (frame)
  191.      FRAME frame;
  192. {
  193.   return frame;
  194. }
  195.  
  196. /* If a machine allows frameless functions, it should define a macro
  197.    FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h.  FI is the struct
  198.    frame_info for the frame, and FRAMELESS should be set to nonzero
  199.    if it represents a frameless function invocation.  */
  200.  
  201. /* Return nonzero if the function for this frame has a prologue.  Many
  202.    machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
  203.    function.  */
  204.  
  205. int
  206. frameless_look_for_prologue (frame)
  207.      FRAME frame;
  208. {
  209.   CORE_ADDR func_start, after_prologue;
  210.   func_start = (get_pc_function_start (frame->pc) +
  211.         FUNCTION_START_OFFSET);
  212.   if (func_start)
  213.     {
  214.       after_prologue = func_start;
  215. #ifdef SKIP_PROLOGUE_FRAMELESS_P
  216.       /* This is faster, since only care whether there *is* a prologue,
  217.      not how long it is.  */
  218.       SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
  219. #else
  220.       SKIP_PROLOGUE (after_prologue);
  221. #endif
  222.       return after_prologue == func_start;
  223.     }
  224.   else
  225.     /* If we can't find the start of the function, we don't really
  226.        know whether the function is frameless, but we should be able
  227.        to get a reasonable (i.e. best we can do under the
  228.        circumstances) backtrace by saying that it isn't.  */
  229.     return 0;
  230. }
  231.  
  232. /* Default a few macros that people seldom redefine.  */
  233.  
  234. #if !defined (INIT_FRAME_PC)
  235. #define INIT_FRAME_PC(fromleaf, prev) \
  236.   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
  237.           prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
  238. #endif
  239.  
  240. #ifndef FRAME_CHAIN_COMBINE
  241. #define    FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
  242. #endif
  243.  
  244. /* Return a structure containing various interesting information
  245.    about the frame that called NEXT_FRAME.  Returns NULL
  246.    if there is no such frame.  */
  247.  
  248. struct frame_info *
  249. get_prev_frame_info (next_frame)
  250.      FRAME next_frame;
  251. {
  252.   FRAME_ADDR address;
  253.   struct frame_info *prev;
  254.   int fromleaf = 0;
  255.  
  256.   /* If the requested entry is in the cache, return it.
  257.      Otherwise, figure out what the address should be for the entry
  258.      we're about to add to the cache. */
  259.  
  260.   if (!next_frame)
  261.     {
  262.       if (!current_frame)
  263.     {
  264.       error ("You haven't set up a process's stack to examine.");
  265.     }
  266.  
  267.       return current_frame;
  268.     }
  269.  
  270.   /* If we have the prev one, return it */
  271.   if (next_frame->prev)
  272.     return next_frame->prev;
  273.  
  274.   /* On some machines it is possible to call a function without
  275.      setting up a stack frame for it.  On these machines, we
  276.      define this macro to take two args; a frameinfo pointer
  277.      identifying a frame and a variable to set or clear if it is
  278.      or isn't leafless.  */
  279. #ifdef FRAMELESS_FUNCTION_INVOCATION
  280.   /* Still don't want to worry about this except on the innermost
  281.      frame.  This macro will set FROMLEAF if NEXT_FRAME is a
  282.      frameless function invocation.  */
  283.   if (!(next_frame->next))
  284.     {
  285.       FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
  286.       if (fromleaf)
  287.     address = next_frame->frame;
  288.     }
  289. #endif
  290.  
  291.   if (!fromleaf)
  292.     {
  293.       /* Two macros defined in tm.h specify the machine-dependent
  294.      actions to be performed here.
  295.      First, get the frame's chain-pointer.
  296.      If that is zero, the frame is the outermost frame or a leaf
  297.      called by the outermost frame.  This means that if start
  298.      calls main without a frame, we'll return 0 (which is fine
  299.      anyway).
  300.  
  301.      Nope; there's a problem.  This also returns when the current
  302.      routine is a leaf of main.  This is unacceptable.  We move
  303.      this to after the ffi test; I'd rather have backtraces from
  304.      start go curfluy than have an abort called from main not show
  305.      main.  */
  306.       address = FRAME_CHAIN (next_frame);
  307.       if (!FRAME_CHAIN_VALID (address, next_frame))
  308.     return 0;
  309.       address = FRAME_CHAIN_COMBINE (address, next_frame);
  310.     }
  311.   if (address == 0)
  312.     return 0;
  313.  
  314.   prev = (struct frame_info *)
  315.     obstack_alloc (&frame_cache_obstack,
  316.            sizeof (struct frame_info));
  317.  
  318.   if (next_frame)
  319.     next_frame->prev = prev;
  320.   prev->next = next_frame;
  321.   prev->prev = (struct frame_info *) 0;
  322.   prev->frame = address;
  323.   prev->next_frame = prev->next ? prev->next->frame : 0;
  324.  
  325. #ifdef INIT_EXTRA_FRAME_INFO
  326.   INIT_EXTRA_FRAME_INFO(fromleaf, prev);
  327. #endif
  328.  
  329.   /* This entry is in the frame queue now, which is good since
  330.      FRAME_SAVED_PC may use that queue to figure out it's value
  331.      (see tm-sparc.h).  We want the pc saved in the inferior frame. */
  332.   INIT_FRAME_PC(fromleaf, prev);
  333.  
  334.   return prev;
  335. }
  336.  
  337. CORE_ADDR
  338. get_frame_pc (frame)
  339.      FRAME frame;
  340. {
  341.   struct frame_info *fi;
  342.   fi = get_frame_info (frame);
  343.   return fi->pc;
  344. }
  345.  
  346. #if defined (FRAME_FIND_SAVED_REGS)
  347. /* Find the addresses in which registers are saved in FRAME.  */
  348.  
  349. void
  350. get_frame_saved_regs (frame_info_addr, saved_regs_addr)
  351.      struct frame_info *frame_info_addr;
  352.      struct frame_saved_regs *saved_regs_addr;
  353. {
  354.   FRAME_FIND_SAVED_REGS (frame_info_addr, *saved_regs_addr);
  355. }
  356. #endif
  357.  
  358. /* Return the innermost lexical block in execution
  359.    in a specified stack frame.  The frame address is assumed valid.  */
  360.  
  361. struct block *
  362. get_frame_block (frame)
  363.      FRAME frame;
  364. {
  365.   struct frame_info *fi;
  366.   CORE_ADDR pc;
  367.  
  368.   fi = get_frame_info (frame);
  369.  
  370.   pc = fi->pc;
  371.   if (fi->next_frame != 0)
  372.     /* We are not in the innermost frame.  We need to subtract one to
  373.        get the correct block, in case the call instruction was the
  374.        last instruction of the block.  If there are any machines on
  375.        which the saved pc does not point to after the call insn, we
  376.        probably want to make fi->pc point after the call insn anyway.  */
  377.     --pc;
  378.   return block_for_pc (pc);
  379. }
  380.  
  381. struct block *
  382. get_current_block ()
  383. {
  384.   return block_for_pc (read_pc ());
  385. }
  386.  
  387. CORE_ADDR
  388. get_pc_function_start (pc)
  389.      CORE_ADDR pc;
  390. {
  391.   register struct block *bl;
  392.   register struct symbol *symbol;
  393.   register struct minimal_symbol *msymbol;
  394.   CORE_ADDR fstart;
  395.  
  396.   if ((bl = block_for_pc (pc)) != NULL &&
  397.       (symbol = block_function (bl)) != NULL)
  398.     {
  399.       bl = SYMBOL_BLOCK_VALUE (symbol);
  400.       fstart = BLOCK_START (bl);
  401.     }
  402.   else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
  403.     {
  404.       fstart = msymbol -> address;
  405.     }
  406.   else
  407.     {
  408.       fstart = 0;
  409.     }
  410.   return (fstart);
  411. }
  412.  
  413. /* Return the symbol for the function executing in frame FRAME.  */
  414.  
  415. struct symbol *
  416. get_frame_function (frame)
  417.      FRAME frame;
  418. {
  419.   register struct block *bl = get_frame_block (frame);
  420.   if (bl == 0)
  421.     return 0;
  422.   return block_function (bl);
  423. }
  424.  
  425. /* Return the blockvector immediately containing the innermost lexical block
  426.    containing the specified pc value, or 0 if there is none.
  427.    PINDEX is a pointer to the index value of the block.  If PINDEX
  428.    is NULL, we don't pass this information back to the caller.  */
  429.  
  430. struct blockvector *
  431. blockvector_for_pc (pc, pindex)
  432.      register CORE_ADDR pc;
  433.      int *pindex;
  434. {
  435.   register struct block *b;
  436.   register int bot, top, half;
  437.   register struct symtab *s;
  438.   struct blockvector *bl;
  439.  
  440.   /* First search all symtabs for one whose file contains our pc */
  441.   s = find_pc_symtab (pc);
  442.   if (s == 0)
  443.     return 0;
  444.  
  445.   bl = BLOCKVECTOR (s);
  446.   b = BLOCKVECTOR_BLOCK (bl, 0);
  447.  
  448.   /* Then search that symtab for the smallest block that wins.  */
  449.   /* Use binary search to find the last block that starts before PC.  */
  450.  
  451.   bot = 0;
  452.   top = BLOCKVECTOR_NBLOCKS (bl);
  453.  
  454.   while (top - bot > 1)
  455.     {
  456.       half = (top - bot + 1) >> 1;
  457.       b = BLOCKVECTOR_BLOCK (bl, bot + half);
  458.       if (BLOCK_START (b) <= pc)
  459.     bot += half;
  460.       else
  461.     top = bot + half;
  462.     }
  463.  
  464.   /* Now search backward for a block that ends after PC.  */
  465.  
  466.   while (bot >= 0)
  467.     {
  468.       b = BLOCKVECTOR_BLOCK (bl, bot);
  469.       if (BLOCK_END (b) > pc)
  470.     {
  471.       if (pindex)
  472.         *pindex = bot;
  473.       return bl;
  474.     }
  475.       bot--;
  476.     }
  477.  
  478.   return 0;
  479. }
  480.  
  481. /* Return the innermost lexical block containing the specified pc value,
  482.    or 0 if there is none.  */
  483.  
  484. struct block *
  485. block_for_pc (pc)
  486.      register CORE_ADDR pc;
  487. {
  488.   register struct blockvector *bl;
  489.   int index;
  490.  
  491.   bl = blockvector_for_pc (pc, &index);
  492.   if (bl)
  493.     return BLOCKVECTOR_BLOCK (bl, index);
  494.   return 0;
  495. }
  496.  
  497. /* Return the function containing pc value PC.
  498.    Returns 0 if function is not known.  */
  499.  
  500. struct symbol *
  501. find_pc_function (pc)
  502.      CORE_ADDR pc;
  503. {
  504.   register struct block *b = block_for_pc (pc);
  505.   if (b == 0)
  506.     return 0;
  507.   return block_function (b);
  508. }
  509.  
  510. /* These variables are used to cache the most recent result
  511.  * of find_pc_partial_function. */
  512.  
  513. static CORE_ADDR cache_pc_function_low = 0;
  514. static CORE_ADDR cache_pc_function_high = 0;
  515. static char *cache_pc_function_name = 0;
  516.  
  517. /* Clear cache, e.g. when symbol table is discarded. */
  518.  
  519. void
  520. clear_pc_function_cache()
  521. {
  522.   cache_pc_function_low = 0;
  523.   cache_pc_function_high = 0;
  524.   cache_pc_function_name = (char *)0;
  525. }
  526.  
  527. /* Finds the "function" (text symbol) that is smaller than PC
  528.    but greatest of all of the potential text symbols.  Sets
  529.    *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
  530.    Returns 0 if it couldn't find anything, 1 if it did.  On a zero
  531.    return, *NAME and *ADDRESS are always set to zero.  On a 1 return,
  532.    *NAME and *ADDRESS contain real information.  */
  533.  
  534. int
  535. find_pc_partial_function (pc, name, address)
  536.      CORE_ADDR pc;
  537.      char **name;
  538.      CORE_ADDR *address;
  539. {
  540.   struct partial_symtab *pst;
  541.   struct symbol *f;
  542.   struct minimal_symbol *msymbol;
  543.   struct partial_symbol *psb;
  544.  
  545.   if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
  546.     {
  547.     if (address)
  548.         *address = cache_pc_function_low;
  549.     if (name)
  550.         *name = cache_pc_function_name;
  551.     return 1;
  552.     }
  553.  
  554.   pst = find_pc_psymtab (pc);
  555.   if (pst)
  556.     {
  557.       if (pst->readin)
  558.     {
  559.       /* The information we want has already been read in.
  560.          We can go to the already readin symbols and we'll get
  561.          the best possible answer.  */
  562.       f = find_pc_function (pc);
  563.       if (!f)
  564.         {
  565.         return_error:
  566.           /* No available symbol.  */
  567.           if (name != 0)
  568.         *name = 0;
  569.           if (address != 0)
  570.         *address = 0;
  571.           return 0;
  572.         }
  573.  
  574.       cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
  575.       cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
  576.       cache_pc_function_name = SYMBOL_NAME (f);
  577.       if (name)
  578.         *name = cache_pc_function_name;
  579.       if (address)
  580.         *address = cache_pc_function_low;
  581.       return 1;
  582.     }
  583.  
  584.       /* Get the information from a combination of the pst
  585.      (static symbols), and the minimal symbol table (extern
  586.      symbols).  */
  587.       msymbol = lookup_minimal_symbol_by_pc (pc);
  588.       psb = find_pc_psymbol (pst, pc);
  589.  
  590.       if (!psb && (msymbol == NULL))
  591.     {
  592.       goto return_error;
  593.     }
  594.       if (psb
  595.       && (msymbol == NULL
  596.           || (SYMBOL_VALUE_ADDRESS (psb)
  597.           >= msymbol -> address)))
  598.     {
  599.       /* This case isn't being cached currently. */
  600.       if (address)
  601.         *address = SYMBOL_VALUE_ADDRESS (psb);
  602.       if (name)
  603.         *name = SYMBOL_NAME (psb);
  604.       return 1;
  605.     }
  606.     }
  607.   else
  608.     /* Must be in the minimal symbol table.  */
  609.     {
  610.       msymbol = lookup_minimal_symbol_by_pc (pc);
  611.       if (msymbol == NULL)
  612.     goto return_error;
  613.     }
  614.  
  615.   {
  616.     if (msymbol -> type == mst_text)
  617.       cache_pc_function_low = msymbol -> address;
  618.     else
  619.       /* It is a transfer table for Sun shared libraries.  */
  620.       cache_pc_function_low = pc - FUNCTION_START_OFFSET;
  621.   }
  622.   cache_pc_function_name = msymbol -> name;
  623.   /* FIXME:  Deal with bumping into end of minimal symbols for a given
  624.      objfile, and what about testing for mst_text again? */
  625.   if ((msymbol + 1) -> name != NULL)
  626.     cache_pc_function_high = (msymbol + 1) -> address;
  627.   else
  628.     cache_pc_function_high = cache_pc_function_low + 1;
  629.   if (address)
  630.     *address = cache_pc_function_low;
  631.   if (name)
  632.     *name = cache_pc_function_name;
  633.   return 1;
  634. }
  635.  
  636. /* Return the innermost stack frame executing inside of the specified block,
  637.    or zero if there is no such frame.  */
  638.  
  639. #if 0    /* Currently unused */
  640.  
  641. FRAME
  642. block_innermost_frame (block)
  643.      struct block *block;
  644. {
  645.   struct frame_info *fi;
  646.   register FRAME frame;
  647.   register CORE_ADDR start = BLOCK_START (block);
  648.   register CORE_ADDR end = BLOCK_END (block);
  649.  
  650.   frame = 0;
  651.   while (1)
  652.     {
  653.       frame = get_prev_frame (frame);
  654.       if (frame == 0)
  655.     return 0;
  656.       fi = get_frame_info (frame);
  657.       if (fi->pc >= start && fi->pc < end)
  658.     return frame;
  659.     }
  660. }
  661.  
  662. #endif    /* 0 */
  663.  
  664. void
  665. _initialize_blockframe ()
  666. {
  667.   obstack_init (&frame_cache_obstack);
  668. }
  669.